/**
 * @author Federico Consoli
 *
 * Released under GNU GPLv3 (http://www.gnu.org/licenses/gpl-3.0.txt)
 */


#include "libreria.h"
#include "Service_2.h"

// Cast for the accept function
#define CAST_ADDR(x) ( struct sockaddr *)(x)
#define cs(x) Connection::char_to_string(x)
#define cus(x) Connection::char_to_uns_short(x)

Service_2 *Ser2=NULL;
Server *registro_locale=NULL;


int main(int argc, const char * argv[]) {
    if ((argc ==2 && argv[1][1]=='h')) {
        cout << "Sintassi: ./server2 <ip_reg> <port_reg> <base_ip> <base_port> <name>\n\n";
        cout << "Parameters are preparatory in order of entry, so you can entry no value\n";
        cout << "Or you can entry only ip_reg or only ip_reg+port_reg etc. etc.\n\n";
        cout << "ip_reg or only ip_reg+port_reg etc. etc.\n\n";
        cout << "ip_reg default: 127.0.0.1\n";
        cout << "port_reg default: 40000\n";
        cout << "base_ip default: 127.0.0.1\n";
        cout << "base_port default: 31000 (Attention here, see doc for info about base_port!)\n";
        cout << "name default: 'SERVER 2' \n\n";
        return 0;
    }
    
    
    try {
         
        
        switch (argc) {
            case 1:
                registro_locale= new Server;
                Ser2= new Service_2;
                break;
            case 2:
                registro_locale= new Server(cs(argv[1])); //string
                Ser2= new Service_2;
                break;
            case 3:
                registro_locale= new Server(cs(argv[1]),cus(argv[2])); //string. u s
                Ser2= new Service_2;
                break;
            case 4:
                registro_locale= new Server(cs(argv[1]),cus(argv[2])); //string. u s
                Ser2= new Service_2(cs(argv[3])); //string
                break;
            case 5:
                registro_locale= new Server(cs(argv[1]),cus(argv[2])); //string. u s
                Ser2= new Service_2(cs(argv[3]),cus(argv[4])); //string us
                break;
            case 6:
                registro_locale= new Server(cs(argv[1]),cus(argv[2])); //string. u s
                Ser2= new Service_2(cs(argv[3]),cus(argv[4]),cs(argv[5])); //string us string
                break;
                
        }
        if (Ser2!=NULL && registro_locale!=NULL)
        Ser2->menu(registro_locale);
        
    } catch (int num_exc) {
        Exception_class::exc(num_exc);
        
    }
    
    delete Ser2;
    delete registro_locale;
    return 0;
}


/**
 * This function chose the local services for the client
 * @param sd socket descriptor of the client
 * @param R pointer to the Responce
 */
 void Server::scegli(int sd, Response *R) {

    //Confronto tra il servizio da avviare e quello ricevuto
    //Il client invia un oggetto di tipo services e vengono controllati TUTTI i parametri

    if (!R->confronto(sd)) {
        cout << display() + "Inconsistent Service Request\n";
        return;
    }
    string Nome = R->testa_elenco_servizi->nome_servizio;
    if (Nome == "Store image") Ser2->store(R, sd);
    else if (Nome == "Get list") Ser2->get_list(sd);
    else if (Nome == "Get image") Ser2->get_img(R, sd);
    else cout << "Service not found\n";
    return;

}